//
//  RW_MixedViewController.m
//  SleepApp
//
//  Created by MAC on 2021/3/22.
//  Copyright © 2021 RiceWang. All rights reserved.
//

#import "RW_MixedViewController.h"
#import "RW_MixedView.h"
#import "RW_SingleModel.h"
#import "RW_UserSingleModel.h"

@interface RW_MixedViewController ()
/** 音频列表 */
@property (nonatomic,strong) NSArray<RW_SongModel *> *songList;

@property (nonatomic,strong) RW_MixedView *wlclass_view;

@property (nonatomic,strong) RW_MixedViewModel *wlclass_view_model;
@end

@implementation RW_MixedViewController
static RW_MixedViewController *class = nil;
#pragma mark -- 单利方法重写 --
+ (instancetype)sharedInstance {
    @synchronized(self) {
        if (class == nil) {
            class = [[self alloc] init];
        }
    }
    return class;
}
#pragma mark -- 功能逻辑方法 --
/** 替换掉当前的播放控制器 */
+(void)realodMixedViewController:(RW_MixedViewController *)vc{
    [class releseViewController];
    class = nil;
    class = vc;
}
/** 停止播放器功能，主要用于和其他类型播放器互斥 */
+(void)stopMixedAudio{
    [[[RW_MixedViewController sharedInstance] wlclass_view_model] stopPlay];
}
/** 当前单利播放器的播放状态 */
+(BOOL)playingStatus{
    return NO;
}
#pragma mark -- 跳转方法 --
/** 打开当前播放列表 */
+(void)pushSingleFromVc:(UIViewController *)fromVc{
    NSLog(@"%@",[RW_PlayerViewController sharedInstance]);
    if (![RW_MixedViewController sharedInstance].wlclass_view_model.mixed_audio_view_array.count){
        [SVProgressHUD showError_wl:@"当前没有正在播放的音频"];
        return;
    }
    [fromVc.navigationController pushViewController:[RW_MixedViewController sharedInstance] animated:YES];
}
/** 打开公共播放列表 */
+(void)pushSingleId:(NSString *)singleId fromVc:(UIViewController *)fromVc{
    [SVProgressHUD show_wl];
    [[[RW_SingleModel getSingleSongListId:singleId] then:^id _Nullable(RW_SingleModel * _Nullable value) {
        if (value.list.count){
            [self showVcSongList:value.list fromVc:fromVc];
        } else {
            [SVProgressHUD showError_wl:@"操作失败"];
        }
        return nil;
    }] catch:^(NSError * _Nonnull error) {
        [SVProgressHUD showError_wl:@"操作失败"];
    }];
}
/** 打开个人播放列表 */
+(void)pushUserSingleId:(NSString *)singleId fromVc:(UIViewController *)fromVc{
    [SVProgressHUD show_wl];
    [[[RW_UserSingleModel getUserSingleSongListId:singleId] then:^id _Nullable(RW_UserSingleModel * _Nullable value) {
        if (value.list.count){
            [self showVcSongList:value.list fromVc:fromVc];
        } else {
            [SVProgressHUD showError_wl:@"操作失败"];
        }
        return nil;
    }] catch:^(NSError * _Nonnull error) {
        [SVProgressHUD showError_wl:@"操作失败"];
    }];
}
//打开一个新的播放器
+(void)showVcSongList:(NSArray<RW_SongModel *> *)songList fromVc:(UIViewController *)fromVc{
    [SVProgressHUD dismiss_wl];
    RW_MixedViewController *vc = [[RW_MixedViewController alloc]init];
    vc.songList = songList;
    [fromVc.navigationController pushViewController:vc animated:YES];
}




#pragma mark -- 页面视图逻辑 --
- (void)initView{
    [super initView];
    [self.contentView addSubview:self.wlclass_view];
    [self.wlclass_view mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.mas_equalTo(self.contentView);
        make.top.mas_equalTo(self.contentView).mas_offset(0);
    }];
}
-(void)initData{
    self.wlclass_view.wlclass_view_model = self.wlclass_view_model;
}

#pragma mark -- 父控制器功能 --
-(UIButton *)set_rightButton{
    UIButton *button = [[UIButton alloc]init];
    [button setTitle:@"选择" forState:UIControlStateNormal];
    return button;
}
-(void)right_button_event:(UIButton *)sender{
    WLLog;
}


#pragma mark -- 懒加载 --
-(RW_MixedView *)wlclass_view{
    if (!_wlclass_view){
        _wlclass_view = [[RW_MixedView alloc]init];
        _wlclass_view.roowViewController = self;
    }
    return _wlclass_view;
}
-(RW_MixedViewModel *)wlclass_view_model{
    if (!_wlclass_view_model){
        _wlclass_view_model = [[RW_MixedViewModel alloc]init];
        _wlclass_view_model.songList = self.songList;
    }
    return _wlclass_view_model;
}


#pragma mark -- 释放内存组件 --
/** 播放器控制器销毁流程 */
-(void)releseViewController{
    if (![[RW_MixedViewController sharedInstance] isEqual:self]){
        [_wlclass_view releaseVM];
        [_wlclass_view removeFromSuperview];
        _wlclass_view.wlclass_view_model = nil;
        _wlclass_view = nil;
        _wlclass_view_model.mixed_audio_view_array = nil;
        _wlclass_view_model.songList = nil;
        _wlclass_view_model = nil;
        _songList = nil;
        NSLog(@"%@",self);
    }
}
@end
